#!/usr/bin/env python3
"""
CNN图像识别器演示脚本
"""

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '.'))

from models.image_classifier import ImageRecognizer
import numpy as np
import torch


def demo():
    """演示函数，展示多种输入格式的使用"""
    
    print("🚀 PyTorch CNN图像识别器演示")
    print("=" * 50)
    
    # 创建识别器（假设是10分类问题）
    recognizer = ImageRecognizer(num_classes=10, image_size=(32, 32))
    
    # 1. 使用随机numpy数组进行演示
    print("\n1. 使用numpy数组输入:")
    numpy_image = np.random.randint(0, 255, (32, 32, 3), dtype=np.uint8)
    result = recognizer.recognize(numpy_image)
    print(f"   预测类别: {result['class']}, 置信度: {result['confidence']:.4f}")
    
    # 2. 使用随机PyTorch tensor进行演示
    print("\n2. 使用PyTorch tensor输入:")
    tensor_image = torch.randn(3, 32, 32)
    result = recognizer.recognize(tensor_image)
    print(f"   预测类别: {result['class']}, 置信度: {result['confidence']:.4f}")
    
    # 3. 使用随机灰度numpy数组
    print("\n3. 使用灰度numpy数组输入:")
    gray_image = np.random.randint(0, 255, (32, 32), dtype=np.uint8)
    result = recognizer.recognize(gray_image)
    print(f"   预测类别: {result['class']}, 置信度: {result['confidence']:.4f}")
    
    # 4. 批量处理演示
    print("\n4. 批量处理演示:")
    batch_inputs = [
        np.random.randint(0, 255, (32, 32, 3), dtype=np.uint8),
        torch.randn(3, 32, 32),
        np.random.randint(0, 255, (32, 32), dtype=np.uint8)
    ]
    batch_results = recognizer.recognize(batch_inputs)
    for i, result in enumerate(batch_results):
        print(f"   第{i+1}个结果 - 类别: {result['class']}, 置信度: {result['confidence']:.4f}")
    
    print("\n✅ 演示完成!")


if __name__ == "__main__":
    # 运行演示
    demo()
    
    print("\n📋 使用说明:")
    print("1. 支持的文件格式: JPEG, PNG, BMP等")
    print("2. 支持的数组格式: numpy数组 (H,W,C) 或 (H,W)")
    print("3. 支持的tensor格式: PyTorch tensor (C,H,W) 或 (H,W)")
    print("4. 支持批量处理: 传入列表即可")
    print("5. 自动处理: 灰度转RGB、调整大小、标准化等")
    
    print("\n💡 实际使用示例:")
    print("""
    # 导入识别器
    from models.image_classifier import ImageRecognizer
    
    # 创建识别器
    recognizer = ImageRecognizer(num_classes=10)
    
    # 识别图像
    result = recognizer.recognize("path/to/image.jpg")
    print(f"预测结果: 类别{result['class']}, 置信度{result['confidence']:.2f}")
    """)